import { isSeq as p, isPair as f, isMap as y } from '../../nodes/identity.js';
import { Pair as l, createPair as u } from '../../nodes/Pair.js';
import { Scalar as g } from '../../nodes/Scalar.js';
import { YAMLSeq as h } from '../../nodes/YAMLSeq.js';
function k(r, i) {
  if (p(r))
    for (let n = 0; n < r.items.length; ++n) {
      let e = r.items[n];
      if (!f(e)) {
        if (y(e)) {
          e.items.length > 1 &&
            i('Each pair must have its own sequence indicator');
          const o = e.items[0] || new l(new g(null));
          if (
            (e.commentBefore &&
              (o.key.commentBefore = o.key.commentBefore
                ? `${e.commentBefore}
${o.key.commentBefore}`
                : e.commentBefore),
            e.comment)
          ) {
            const m = o.value ?? o.key;
            m.comment = m.comment
              ? `${e.comment}
${m.comment}`
              : e.comment;
          }
          e = o;
        }
        r.items[n] = f(e) ? e : new l(e);
      }
    }
  else i('Expected a sequence for this tag');
  return r;
}
function w(r, i, n) {
  const { replacer: e } = n,
    o = new h(r);
  o.tag = 'tag:yaml.org,2002:pairs';
  let m = 0;
  if (i && Symbol.iterator in Object(i))
    for (let t of i) {
      typeof e == 'function' && (t = e.call(i, String(m++), t));
      let c, s;
      if (Array.isArray(t))
        if (t.length === 2) (c = t[0]), (s = t[1]);
        else throw new TypeError(`Expected [key, value] tuple: ${t}`);
      else if (t && t instanceof Object) {
        const a = Object.keys(t);
        if (a.length === 1) (c = a[0]), (s = t[c]);
        else
          throw new TypeError(
            `Expected tuple with one key, not ${a.length} keys`,
          );
      } else c = t;
      o.items.push(u(c, s, n));
    }
  return o;
}
const E = {
  collection: 'seq',
  default: !1,
  tag: 'tag:yaml.org,2002:pairs',
  resolve: k,
  createNode: w,
};
export { w as createPairs, E as pairs, k as resolvePairs };
